package com.zwj.spring_study.a04;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

/**
 * bean后处理器的作用
 *
 * @author 张文接
 * @version 1.0
 * @createTime 2024/5/26 22:04
 */
@Slf4j
@SpringBootApplication
public class A04Application {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();

        //注册bean到容器中
        context.registerBean(Bean1.class);
        context.registerBean(Bean2.class);
        context.registerBean(Bean3.class);
        context.registerBean(Bean4.class);

        //注册解析@Autowired @Value注解的postProcessor
        context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);

        //注册解析公共注解的postProcessor 包括：@PostConstruct @PreDestroy @Resource
        context.registerBean(CommonAnnotationBeanPostProcessor.class);

        //注册解析注解@ConfigurationProperties 的postProcessor
        ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());
        //初始化容器
        context.refresh();

        System.out.println(context.getBean(Bean4.class));
        //关闭容器
        context.close();
    }
}
